package vo;

import entity.Item;
import entity.Product;
import util.JdbcUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Cart implements Serializable {

    private List<Item> items=new ArrayList<>();
    private Double price;

    public Cart() {
        price=0.0;
    }

    public void add(Integer productId){
        Product product=findProductById(productId);

        price+=product.getPrice();

        for (Item item : items) {
            if(item.getProduct().getId().equals(productId)){
                item.setNum(item.getNum()+1);
                item.setPrice(item.getPrice()+product.getPrice());
                return;
            }
        }

        Item item=new Item();
        item.setProduct(product);
        item.setNum(1);
        item.setPrice(product.getPrice());
        items.add(item);
    }


    public void removeByProductId(Integer productId){
        for (int i = 0; i < items.size(); i++) {
            Item item=items.get(i);
            if(item.getProduct().getId().equals(productId)){
                price-=item.getPrice();
                items.remove(item);
            }
        }
    }

    public void removeByProductIds(List<Integer> productIds){
        for (Integer productId : productIds) {
            removeByProductId(productId);
        }
    }

    public void clear(){
        items=new ArrayList<>();
        price=0.0;
    }

    public void modify(Integer productId,Integer num){

        Product product=findProductById(productId);

        for (Item item : items) {
            if(item.getProduct().getId().equals(productId)){
                price-=item.getPrice();
                item.setNum(num);
                item.setPrice(product.getPrice()*num);
                price+=item.getPrice();
            }
        }

    }

    private Product findProductById(Integer id){
      Connection con=null;
      PreparedStatement ps=null;
      ResultSet rs=null;
      Product product=null;

      try{

          con=JdbcUtil.getConnection();

          con.setAutoCommit(false);


          String sql=new StringBuffer()
                  .append("select * ")
                  .append("from t_product ")
                  .append("where id=? ")
                  .toString();

          ps=con.prepareStatement(sql);

          ps.setInt(1,id);

          rs=ps.executeQuery();

          if(rs.next()){
              product=new Product();
              product.setId(rs.getInt("id"));
              product.setName(rs.getString("name"));
              product.setPrice(rs.getDouble("price"));
          }

          con.commit();

      }catch (Exception e){
          e.printStackTrace();
          try {
              con.rollback();
          } catch (SQLException e1) {
              e1.printStackTrace();
          }
      }finally{
          JdbcUtil.close(con,ps,rs);
      }


      return product;

    }


    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }
}
